//
//  ZCInvolvedcommentsCell.m
//  zctx
//
//  Created by apple on 15/12/28.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCInvolvedcommentsCell.h"

@implementation ZCInvolvedcommentsCell

- (void)awakeFromNib {
    // Initialization code
    
    self.contentLable.preferredMaxLayoutWidth = SCREEN_WIDTH - 89;
    self.zanNumLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
    self.comNumLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
    self.nameLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
    self.timeLable.textColor = [UIColor colorWithHexString:@"#b2b2b2"];
    self.contentLable.textColor = [UIColor colorWithHexString:@"#404040"];
    self.numLable.textColor = [UIColor colorWithHexString:@"#ffdd00"];
    self.diLine.backgroundColor = RGB(236, 237, 238);
    self.diLineContraint.constant = 0.5;
    self.iconImgView.layer.masksToBounds = YES;
    self.iconImgView.layer.cornerRadius = 45/2;
    
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zanButtonClick)];
    [self.zanView addGestureRecognizer:recognizer];
    
    UITapGestureRecognizer *recognizercomment = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(commentButtonClick)];
    [self.commentView addGestureRecognizer:recognizercomment];
    
}

-(void)setIndexPach:(NSIndexPath *)indexPach{
    _indexPach = indexPach;
}

-(void)setModel:(ZCInvolvedDetailsModelHotComments *)model{
    _model = model;
    
    NSLog(@"adsfadf = %@",model.username);
    self.nameLable.text = model.username;
    
    self.comNumLable.text = model.commentCount;
    int timeInt =[model.ctime intValue];
    self.timeLable.text = [NSString stringWithFormat:@"%@",stringDataWithTimeInterval(timeInt)];
    self.contentLable.text = model.content;
    
    if ([model.likecount integerValue]>99) {
        self.zanNumLable.text = [NSString stringWithFormat:@"99+"];
    }else{
        self.zanNumLable.text = model.likecount;
    }
    
    if ([model.commentCount integerValue]>99) {
        self.comNumLable.text = [NSString stringWithFormat:@"99+"];
    }else{
        self.comNumLable.text = model.commentCount;
    }
    if (model.avatar.length>0) {
        
        [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"projectdetail_icon"]];
    }else{
        self.iconImgView.image = [UIImage imageNamed:@"projectdetail_icon"];
    }

    UIImage *image = _model.islick ? [self likeImage] : [self unlikeImage];
    [self.zanImgView setImage:image];
    
    if (_model.islick ==YES) {
        self.zanNumLable.textColor = [UIColor colorWithHexString:@"#ffdd00"];
        UIImage *image = [self likeImage];
        [self.zanImgView setImage:image];
    }else
    {
        self.zanNumLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
        UIImage *image = [self unlikeImage];
        [self.zanImgView setImage:image];
    }
}

- (void)zanButtonClick{
    
    if (UserModel.password.length>1) {
        NSInteger num = [_model.likecount integerValue];
        _model.islick = YES;
        num++;
        
        if (num>99) {
            self.zanNumLable.text = [NSString stringWithFormat:@"99+"];
        }else{
            self.zanNumLable.text = [NSString stringWithFormat:@"%ld",(long)num];
        }
        _model.likecount = [NSString stringWithFormat:@"%ld",(long)num];
        self.zanView.userInteractionEnabled = NO;
        UIImage *image = _model.islick ? [self likeImage] : [self unlikeImage];
        
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            self.zanImgView.layer.transformScale = 1.7;
            self.numLable.alpha = 1;
            self.zanNumLable.textColor = [UIColor colorWithHexString:@"#ffdd00"];
        } completion:^(BOOL finished) {
            
            [self.zanImgView setImage:image];
            [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                self.zanImgView.layer.transformScale = 0.9;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                    self.numLable.alpha = 0;
                    self.zanImgView.layer.transformScale = 1;
                    
                } completion:^(BOOL finished) {
                }];
            }];
        }];
        
        if (_delegate && [ _delegate respondsToSelector:@selector(likeButtonClickindex:islogin:)]) {
            [_delegate likeButtonClickindex:_indexPach islogin:YES];
        }
    }else{
        if (_delegate && [ _delegate respondsToSelector:@selector(likeButtonClickindex:islogin:)]) {
            [_delegate likeButtonClickindex:_indexPach islogin:NO];
        }
    }
}

- (void)commentButtonClick {
    if (_delegate && [ _delegate respondsToSelector:@selector(commentButtonClickindex:)]) {
        [_delegate commentButtonClickindex:_indexPach];
    }
}

- (UIImage *)likeImage {
    static UIImage *img;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        img = [UIImage imageNamed:@"projectdetail_zan_yes"];
    });
    return img;
}

- (UIImage *)unlikeImage {
    static UIImage *img;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        img = [UIImage imageNamed:@"projectdetail_zan"];
    });
    return img;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
